<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>活跃用户数明细表</title>
  <link rel="stylesheet" href="../../mobile_static/css/element.css">
  <link rel="stylesheet" href="../../mobile_static/css/base.css">
  <link rel="stylesheet" href="../../mobile_static/css/trafficMetrics-headquarters/activeUsersList.css">
  <script src="../../mobile_static/js/vue.js"></script>
  <script src="../../mobile_static/js/axios.js"></script>
</head>

<body>
  <div id="app">
    <div class="app-content">
      <div class="top-bar">
        <div class="top-bar_left">
          <ul>
            <li>
              <el-button @click="backHome" type="primary" plain>返回</el-button>
            </li>
            <li :class="{'selected':isSelect == 1}" @click="isSelect=1">昨日</li>
            <li :class="{'selected':isSelect == 2}" @click="isSelect=2">近7天</li>
            <li :class="{'selected':isSelect == 3}" @click="isSelect=3">近30天</li>
            <li :class="{'selected':isSelect == 4}" @click="isSelect=4">本年</li>
          </ul>
          <div class="date-box">
            <el-date-picker value-format="yyyy-MM-dd" v-model="timeValue" @change="dateChange" type="daterange"
              unlink-panels range-separator="至" start-placeholder="请选择开始日期" end-placeholder="请选择结束日期">
            </el-date-picker>
          </div>
        </div>
        <div class="top-bar_right">
        </div>
      </div>

      <!-- 表格 -->
      <div class="table-box">
        <!-- 表格标题 -->
        <div class="table-title flex between middle search-box-list">
          <h2>活跃用户数明细表</h2>
          <div class="search-box">
            <el-input prefix-icon="el-icon-search" style="width:200px;" v-model="searchValue" placeholder="按用户名称搜索" @change="search"></el-input>
            <el-select filterable v-model="value" placeholder="请选择" @change="search" :disabled="jurisdiction">
              <el-option v-for="item in options" :key="item.g0departmentcode" :label="item.g0departmentname"
                :value="item.g0departmentcode">
              </el-option>
            </el-select>
            <el-button type="primary" @click="download">下载</el-button>
          </div>
        </div>
        <!-- 表格主体 -->
        <div class="table-main">
          <el-table :data="tableData" stripe max-height="600" style="width: 100%" v-loading="loading" border :header-cell-style="{background: 'linear-gradient(180deg, #FAFAFA 0%, #F1F1F1 100%)',fontSize:'16px',fontWeight:'bold',color:'#000000'}">
            <el-table-column prop="g0userid" label="登录账号" width="160">
            </el-table-column>
            <el-table-column prop="g0username" label="用户姓名" width="120">
            </el-table-column>
            <el-table-column prop="g0full_department_name" label="所属企业" min-width="260">
            </el-table-column>
            <el-table-column label="最近访问时间" prop="g0lastaccesstime" width="200">
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="paging-box">
          <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="thisPage" :page-sizes="[10, 20, 30, 40]" :page-size="size"
              layout="total, sizes, prev, pager, next, jumper" :total="length">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<script src="../../mobile_static/js/element.js"></script>
<script type="module">
  import { postOneId, getOneIdDownload } from '../../mobile_static/axios/axios.config.js'

  new Vue({
    el: "#app",
    data() {
      return {
        loading: false,

        timeValue: "", //日期value值
        isSelect: 5,
        options: [
         
        ],
        value: '', //选择器value值
        searchValue: "",
        startDate: "2021-01-01",
        endDate: "2022-03-08",
        size: 10,
        thisPage: 1,
        tableData: [],
        length: "",
        jurisdiction:false
      }
    },
    created() {
      this.options = JSON.parse(sessionStorage.getItem('codeAll'))

      if (window.location.search == "") {
        this.startDate = "2016-01-01"
        this.endDate = "2022-01-01"
      } else if (window.location.search.slice(1).split("=")[0] == "code") {
        this.isSelect = sessionStorage.getItem('isSelect')
        this.value = window.location.search.slice(1).split("&")[0].split("=")[1]
        this.startDate = window.location.search.slice(1).split("&")[1].split("=")[1]
        this.endDate = window.location.search.slice(1).split("&")[2].split("=")[1]
        if (sessionStorage.getItem('isSelect') == 5) {
          this.timeValue = [this.startDate, this.endDate]
        }
      }
      if(sessionStorage.getItem('jurisdiction')==1){
        this.jurisdiction = false
      }else{
        this.jurisdiction = true
      }
    },
    mounted() {
      // this.RequestActiveUser()
    },
    watch: {
      //针对日期改变做监听
      isSelect(val) {
        this.thisPage = 1
        let nowDate = new Date();
        if (val != 5) {
          this.endDate = `${nowDate.getFullYear()}-${this.ZeroComplement(nowDate.getMonth() + 1)}-${this.ZeroComplement(nowDate.getDate())}`
        }
        if (val == 1) {
          this.timeValue = []
          if (nowDate.getDate() == 1) {
            if (nowDate.getMonth() == 1 || 3 || 5 || 7 || 8 || 10 || 12) {
              this.startDate = `${nowDate.getFullYear()}-${this.ZeroComplement(nowDate.getMonth() + 1)}-31`
            } else if (nowDate.getMonth() == 4 || 6 || 9 || 11) {
              this.startDate = `${nowDate.getFullYear()}-${this.ZeroComplement(nowDate.getMonth() + 1)}-30`
            } else {
              this.startDate = `${nowDate.getFullYear()}-${this.ZeroComplement(nowDate.getMonth() + 1)}-28`
            }
          } else {
            this.startDate = `${nowDate.getFullYear()}-${this.ZeroComplement(nowDate.getMonth() + 1)}-${this.ZeroComplement(nowDate.getDate() - 1)}`
          }
          // this.RequestFourCrazy()
        } else if (val == 2) {
          this.timeValue = []
          var date1 = new Date();
          //今天时间
          var time1 = date1.getFullYear() + "-" + this.ZeroComplement(date1.getMonth() + 1) + "-" + date1.getDate()
          var date2 = new Date(date1);
          date2.setDate(date1.getDate() + -7);
          //num是正数表示之后的时间，num负数表示之前的时间，0表示今天
          var time2 = date2.getFullYear() + "-" + this.ZeroComplement(date2.getMonth() + 1) + "-" + this.ZeroComplement(date2.getDate());
          this.startDate = time2
          // this.RequestFourCrazy()
        } else if (val == 3) {
          this.timeValue = []
          let day_30 = new Date().setMonth((new Date().getMonth() - 1));    //日期是30天前的对应时间戳
          day_30 = new Date(day_30);       //转换成标准时间
          let y = day_30.getFullYear();    //年
          let m = day_30.getMonth() + 1;    //月
          let d = day_30.getDate();        //日
          if (m < 10) {
            m = '0' + m;
          }
          if (d < 10) {
            d = '0' + d;
          }
          day_30 = y + '-' + m + '-' + d;    //日期是30天前的对应字符串 xxxx-xx-xx　
          this.startDate = day_30
          // this.RequestFourCrazy()
        } else if (val == 4) {
          this.timeValue = []
          this.startDate = nowDate.getFullYear() + "-01-01"
          // this.RequestFourCrazy()
        }
        this.RequestActiveUser()
      }
    },
    methods: {
      search() {
        this.thisPage = 1
        this.RequestActiveUser()
      },
      backHome() {
        window.history.back()
      },
      handleSizeChange(val) {
        this.size = val
        this.RequestActiveUser()
      },
      handleCurrentChange(val) {
        this.thisPage = val
        this.RequestActiveUser()
      },
      //日期选择框
      dateChange() {
        this.isSelect = 5
        this.startDate = this.timeValue[0]
        this.endDate = this.timeValue[1]
        this.RequestActiveUser()
      },
      //请求活跃用户
      RequestActiveUser() {
        this.loading = true
        postOneId("hyyhsmxb00", {
          pageSize: this.size,
          pageNum: this.thisPage,
          startDate: this.startDate,
          endDate: this.endDate,
          g0enterpriseCode: this.value,
          g0username: this.searchValue
        }).then(res => {
          if (!res) {
            return
          }
          this.loading = false
          res = JSON.parse(res.data.msg);
          // console.log(res, "请求活跃用户成功");
          if (res.total) {
            this.tableData = res.data
          } else {
            this.tableData = []
          }
          this.length = res.total
        })
      },
      download() {
        if (this.length > 50000) {
          this.$notify.error({
            title: '下载失败',
            message: "下载数据条数不能超过50000条",
          });
          return
        }
        this.loading = true
        getOneIdDownload("hyyhsmxb00Export", {
          pageSize: this.length,
          pageNum:1,
          startDate: this.startDate,
          endDate: this.endDate,
          g0enterpriseCode: this.value,
          g0username: this.searchValue
        }).then(res => {
          if (!res) {
            return
          }
          this.loading = false

          let url = window.URL.createObjectURL(res.data)
          let link = document.createElement("a")
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '活跃用户数明细表.xlsx')
          document.body.appendChild(link)
          link.click()
        })
      },
      //补零函数
      ZeroComplement(num) {
        if (num < 10) {
          return "0" + num;
        } else {
          return num;
        }
      }
    }
  })
</script>